package com.tom.architect.party3.day26.okhttp;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


// 线程池处理
public class Dispatcher {

    private ExecutorService executorService;

    public synchronized ExecutorService executorService() {
        if (executorService == null) {
            // Util.threadFactory("OkHttp Dispatcher", false) 线程工厂
            executorService = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60, TimeUnit.SECONDS,
                    new SynchronousQueue<Runnable>(),
                    new ThreadFactory() {
                        @Override
                        public Thread newThread(Runnable r) {
                            Thread thread = new Thread(r, "OkHttp");
                            thread.setDaemon(false); // 非守护线程
                            return thread;
                        }
                    });
        }
        return executorService;
    }

    public void enqueue(RealCall.AsyncCall call) {
        executorService().execute(call);
    }


}
